home *** CD-ROM | disk | FTP | other *** search
-
- typedef int T;
-
- // Boolean type
- typedef int Truth;
-
- // Node of T is a parametric type.
- // A node contains a value of type T. Each node can link
- // itself to another node
- class Node {
- public:
- Node( T x) { val = x; Next = 0; }
- Node *next() { return Next; }
- void link( Node *neighbor) { Next = neighbor; }
- T value() { return val; }
- private:
- Node *Next;
- T val;
- };
-
- // List of T is a Lisp-style list of T values.
- class List {
- public:
- List() { head = 0; }
- ~List() {}
-
- Truth isempty() { return head == 0; }
-
- // get returns the value at the head of the List
- // without deleting it. This is car in LISP.
- // precondition: !isempty() (List is not empty)
- T get() { return head->value(); }
-
- // add inserts a new element with value x at the head
- // of the List
- void add(T x)
- {
- Node *p = new Node(x); // error return of new should
- // be checked ...
- if( !isempty() ) // member fun. calls member fun.
- p->link( head);
- head = p;
- }
-
- // del deletes value at the head of the list
- // and returns the value
- // precondition: !isempty() (List is not empty)
- T del()
- {
- Node *t = head;
- T r = head->value();
- head = head->next();
- delete t;
- return r;
- }
- private:
- Node *head;
- };
-
-